﻿using Basic.FlowModel.DB;
using Basic.FlowModel.Node;
using Basic.FlowRemoteModel;
using Basic.FlowRemoteModel.FlowNode.Model;
using WeDonekRpc.Helper;
using WeDonekRpc.Helper.IdGenerator;
using WeDonekRpc.SqlSugar;

namespace Basic.FlowDAL.lmpl
{
    internal class FlowNodeDAL : BasicDAL<DBFlowNode, long>, IFlowNodeDAL
    {
        public FlowNodeDAL ( IRepository<DBFlowNode> basicDAL ) : base(basicDAL, "node")
        {
        }
        public ToNoteBase[] GetsToNode ( long id )
        {
            return this._BasicDAL.Join<DBFlowNodeRoute, ToNoteBase>(( a, b ) => a.Id == b.ToNodeId && b.NodeId == id, ( a, b ) => new ToNoteBase
            {
                Id = a.Id,
                IntoFormula = a.IntoFormula
            });
        }

        public void Set ( DBFlowNode sour, FlowNodeForm set )
        {
            ISqlQueue<DBFlowNode> queue = this._BasicDAL.BeginQueue();
            queue.Update(new DBFlowNode
            {
                Id = sour.Id,
                FormId = set.FormId,
                IsAllowEdit = set.IsAllowEdit
            }, "FormId", "IsAllowEdit");
            queue.DeleteBy<DBFlowNodeFormPower>(a => a.NodeId == sour.Id);
            queue.DeleteBy<DBFlowFormDataRelation>(a => a.NodeId == sour.Id);
            _ = queue.Submit();
        }
        public long Add ( FlowNodeAdd data, string code )
        {
            DBFlowNode add = new DBFlowNode
            {
                Id = IdentityHelper.CreateId(),
                AuditType = FlowAuditType.独立审批,
                IsAllowCancel = false,
                NodeCode = code,
                NodeType = data.NodeType,
                FlowId = data.FlowId,

                NodeName = data.NodeName,
                Coordinate = data.Coordinate
            };
            if ( data.NodeType != FlowNodeType.结束节点 )
            {
                ISqlQueue<DBFlowNode> queue = this._BasicDAL.BeginQueue();
                queue.Insert(add);
                queue.InsertBy(new DBFlowNodeAuditEmp
                {
                    Id = IdentityHelper.CreateId(),
                    Show = "自由选择",
                    SpecifyMode = FlowSpecifyMode.自由指定,
                    FlowId = data.FlowId,
                    NodeId = add.Id,
                });
                _ = queue.Submit();
            }
            else
            {
                this._BasicDAL.Insert(add);
            }
            return add.Id;
        }

        public void SetCoordinate ( DBFlowNode sour, ImgPoint point )
        {
            if ( !this._BasicDAL.Update(a => a.Coordinate == point, a => a.Id == sour.Id) )
            {
                throw new ErrorException("flow.node.set.fail");
            }
        }

        public void Set ( ToNoteBase[] datas )
        {
            ISqlQueue<DBFlowNode> queue = this._BasicDAL.BeginQueue();
            datas.ForEach(a =>
            {
                queue.Update(new DBFlowNode
                {
                    Id = a.Id,
                    OutWaitNodeId = a.OutWaitNodeId,
                    BackNodeId = a.BackNodeId,
                    IntoFormula = a.IntoFormula
                }, new string[] { "OutWaitNodeId", "BackNodeId", "IntoFormula" });
            });
            _ = queue.Submit();
        }

        public void SetIntoFormula ( long id, IntoFormula[] intoFormula )
        {
            if ( !this._BasicDAL.Update(a => a.IntoFormula == intoFormula, a => a.Id == id) )
            {
                throw new ErrorException("flow.node.set.fail");
            }
        }
    }
}
